package models

import (
	"box-serve/boxserverpc/boxserverpc"

	"gorm.io/gorm"
)

type Shop struct {
	gorm.Model
	Title   string  `gorm:"type:varchar(20);not null;"`
	Price   float64 `gorm:"type:decimal(10,2);not null;"`
	Image   string  `gorm:"type:varchar(500);not null;"`
	Info    string  `gorm:"type:varchar(300);not null;"`
	ClassId int     `gorm:"type:int(10);not null;"`
}

func (s *Shop) ShopAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

func (s *Shop) FindShopByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(s).Error
}

func (s *Shop) ShopList(db *gorm.DB, lists []*boxserverpc.ShopList, in *boxserverpc.ShopListRequest) ([]*boxserverpc.ShopList, error) {

	err2 := db.Debug().Model(&Shop{}).
		Select("shops.*, classes.title as class_title,shops.id as shop_id").
		Joins("LEFT JOIN classes ON classes.id = shops.class_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error

	return lists, err2
}

type Class struct {
	gorm.Model
	Title string `gorm:"type:varchar(20);not null;"`
}

func (c *Class) FindClassByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(c).Error
}

func (c *Class) ClassAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}
